package cn.cosmosx.base.pdf.flow;

import cn.cosmosx.base.pdf.PDFTableReader;
import cn.cosmosx.base.pdf.RegExUtil;
import cn.cosmosx.base.pdf.bean.AlipayFlow;

import java.io.File;
import java.util.List;

public class AlipayFlowUtil {
    // 注：正则 “?<=RegEx”：匹配RegEx之后的内容；“?=RegEx”：匹配RegEx之前的内容
    private static final String NAME_REG = "(?<=[：|:])\\S+(?=[\\(|（])";
    private static final String ID_REG = "(?<=(\\(\\S{1,10}[：|:]))\\S+(?=[\\)|）])";
    private static final String ACCOUNT_REG = "(?<=(\\u8d26\\u53f7|\\u5fae\\u4fe1\\u53f7：))\\S+(?=[\\u4e2d])";
    private static final String DATE_REG = "(\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2})";

    public static FlowWrapper<AlipayFlow> read(String pdfFilePath) {
        return read(new File(pdfFilePath));
    }

    public static FlowWrapper<AlipayFlow> read(File file) {
        PDFTableReader<AlipayFlow> reader = PDFTableReader.load(file, AlipayFlow.class)
                .describe(0, 3, 6)
                .titleRowIndex(2);
        List<AlipayFlow> list = reader.readBeans();
        String keyInfo = reader.getKeyInfo();
        String realName = RegExUtil.find(NAME_REG, keyInfo);
        String idCardNumber = RegExUtil.find(ID_REG, keyInfo);
        String account = RegExUtil.find(ACCOUNT_REG, keyInfo);
        String[] dateArr = RegExUtil.findMulti(DATE_REG, keyInfo);
        String startDate = null;
        String endDate = null;
        if (dateArr.length > 1) {
            startDate = dateArr[0];
            endDate = dateArr[1];
        }
        return new FlowWrapper<>(realName, idCardNumber, account, startDate, endDate, list);
    }
}
